package com.wchat.network.device;

import java.io.Serializable;
import java.net.InetAddress;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

import com.wchat.network.Configs;
@Root
public class Device extends AbstractSendTarget implements Serializable {
	
	private static final long serialVersionUID = -2887923739581540671L;

	@Attribute
	private long id;

	@Attribute
	private String name;
	
	private long generateTime;
	
	public Device() {
		super(null);
	}
	
	public Device(InetAddress address) {
		super(address);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void refresh() {
		generateTime = System.currentTimeMillis();
	}

	public boolean isExpired() {
		return System.currentTimeMillis() - generateTime > Configs.EXPIRE_TIME;
	}

	@Override
	public String toString() {
		return "Device [id=" + id + ", name=" + name + ", address=" + address + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Device other = (Device) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}


}
